from flask import Flask
from flask import request

import json
import os

from src.ucloud.ufile_log_list import read_log_list
from src.ucloud.ufile_log_list import read_log_list_page
from src.ucloud.read_log_file import read_log_file
from src.parser.xlog_parser import parse_xlog_to_log

# from tornado.wsgi import WSGIContainer
# from tornado.web import FallbackHandler
# # import tornado.web
# from tornado.ioloop import IOLoop
# from src.websocket_tornado import MyApplication, EchoWebSocket

app = Flask(__name__)
"""
这种方式请求开始放弃
"""


@app.route('/')
def hello_world():
    log_list = read_log_list()
    resp = {'code': 0, 'data': log_list}
    return json.dumps(resp)


@app.route('/list', methods={"POST"})
def log_list():
    page_last_file = ''
    page_size = 30
    prefix = ''

    json_data = request.get_json()
    if json_data is not None:
        parms = json_data['parms']
        if parms is not None:
            page_last_file = parms['page_last_file']
            page_size = parms['page_size']
            prefix = parms['prefix']
        else:
            resp = {'code': 500, 'msg': "parms error"}
            return json.dumps(resp)
    else:
        resp = {'code': 500, 'msg': "parms error"}
        return json.dumps(resp)
    print(json_data)
    log_list, is_next = read_log_list_page(page_last_file, page_size, prefix)
    resp = {'code': 0, 'data': log_list, 'next': is_next}
    return json.dumps(resp)


@app.route('/readFile', methods={"POST"})
def log_query_by_file():
    read_file = ''
    json_data = request.get_json()
    if json_data is not None:
        parms = json_data['parms']
        if parms is not None:
            read_file = parms['read_file']
        else:
            resp = {'code': 500, 'msg': "parms error"}
            return json.dumps(resp)
    else:
        resp = {'code': 500, 'msg': "parms error"}
        return json.dumps(resp)
    read_file_path = read_log_file(read_file)

    file_text = ''
    if read_file_path is not None:
        try:
            ext = read_file_path.split('/')[-1].split('.')[-1]
            if ext == 'log':
                file_object = open(read_file_path, 'r', encoding='utf-8')
                try:
                    file_text = file_object.read()
                finally:
                    file_object.close()
                    os.remove(read_file_path)
            elif ext == 'xlog':
                # 解析
                parse_xlog_to_log(read_file_path)
                file_object = open(read_file_path + '.log', 'r', encoding='utf-8')
                try:
                    file_text = file_object.read()
                finally:
                    file_object.close()
                    os.remove(read_file_path)
                    os.remove(read_file_path + '.log')
            else:
                # jpg ogg wan 等格式
                pass
        except Exception as e:
            pass

    resp = {'code': 0, 'data': file_text}
    return json.dumps(resp)


if __name__ == '__main__':
    app.run()


